#!/usr/bin/php
<?php
function generateuniquenumbers($len,$count,$includezero=1)
{
  if( strlen($count) > $len )
  {
        echo "Looks something funny in the parameter which you have supplied. \n";
	return ;
  }
  echo "Generating " . $count . " numbers with length " . $len . "\n";
  $mult = 0;
  $fract = 0;
  $numbers[] = "";
  mt_srand(10);
  if( $len > 9 )
  {
      $mult = (int)($len/9);
      $fract = (int) $len % 9;
  } else {
      $fract = $len;    
  }  
  if( $fract > 0 )
  {
	$minfract = "";
	$maxfract = "";
	for($m=0; $m<$fract; $m++)
	{
	  if($includezero)
	   $minfract = "0";
	  else
	   $minfract .= "1";
	   $maxfract .= "9";
	}	
  }
	
  for($numcount=0;$numcount<$count;$numcount++)
  {
     $curnumber = "";
	  for($i=0;$i<$mult;$i++)
	  {
		if( $includezero )
			$currand = mt_rand(0,999999999);
		else
			$currand = mt_rand(111111111,999999999);
		if( strlen($currand) < 9 )
		{
			$templen = strlen($currand);
			for( $pad = 9 - $templen; $pad > 0; $pad--)
				$curnumber .= "0";
		}
		$curnumber .= $currand;
	  }
	  if( $fract > 0 )
	  {
		$currand = mt_rand($minfract,$maxfract);
		if( strlen($currand) < strlen($maxfract) )
		{
			$templen = strlen($currand);
			for( $pad=strlen($maxfract)-$templen;$pad > 0;$pad--)
				$curnumber .= "0";
		}
		$curnumber .= $currand;
	  }
      if( !in_array($curnumber,$numbers) )
	{
		$numbers[] = $curnumber;
	}
      else
		$numcount --;
   }
   return $numbers;	  
}

$ncount = 20;
$nlength = 20;

$numbers = generateuniquenumbers($nlength,$ncount,0);

for($k=1;$k<=$ncount;$k++)
   echo  $numbers[$k] . "\n";


?>
